package com.tstudy.laoshibang.custom;

import java.util.Calendar;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.utils.CommonUtil;

import android.view.View;

public class BirthdaySelectView {

	private View view;
	private WheelView yearView;
	private WheelView monthView;
	private WheelView dayView;
	int[] yearItems;
	int[] monthItems;
	int[] dayItems;
	int selectYearPosition;
	int selectMonthPosition;
	int selectDayPosition;

	int selectYear;
	int selectMonth;
	int selectDay;
	
	NumericWheelAdapter yearAdapter = new NumericWheelAdapter();
	NumericWheelAdapter monthAdapter = new NumericWheelAdapter();
	NumericWheelAdapter dayAdapter = new NumericWheelAdapter();
	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public BirthdaySelectView(View view) {
		super();

		this.view = view;
		setView(view);
	}

	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	public void initDate() {
		yearView = (WheelView) view.findViewById(R.id.date_select_dialog_year);
		monthView = (WheelView) view.findViewById(R.id.date_select_dialog_month);
		dayView = (WheelView) view.findViewById(R.id.date_select_dialog_day);
		int currentYear = Calendar.getInstance().get(Calendar.YEAR);
		yearAdapter.setDate(1900,currentYear);
		monthAdapter.setDate(1, 12);
		int daycount = CommonUtil.getDayCount(currentYear, 1);
		this.yearItems = CommonUtil.buildData(1900, currentYear);
		this.monthItems = CommonUtil.buildData(1, 12);
		this.dayItems = CommonUtil.buildData(1, 31);
		dayAdapter.setDate(1,daycount);
		yearView.setAdapter(yearAdapter);
		monthView.setAdapter(monthAdapter);
		dayView.setAdapter(dayAdapter);

		yearView.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				selectYear = newValue;
				int daycount = CommonUtil.getDayCount(selectYear, selectMonth);
				dayAdapter.setDate(1, daycount);
				dayView.setCurrentItem(Math.min(selectDay, daycount));
			}
		});
		monthView.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				selectMonth = newValue;
				int daycount = CommonUtil.getDayCount(selectYear, selectMonth);
				dayAdapter.setDate(1, daycount);
				dayView.setCurrentItem(Math.min(selectDay, daycount));
			}
		});
		dayView.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				selectDay = newValue;
				
			}
		});
		
		yearView.setCyclic(true);// 可循环滚动
		monthView.setCyclic(true);
		dayView.setCyclic(true);
		yearView.setLabel("年");// 添加文字
		monthView.setLabel("月");// 添加文字
		dayView.setLabel("日");// 添加文字
		yearView.setCurrentItem(dayItems[dayItems.length-1]);// 初始化时显示的数据
		monthView.setCurrentItem(0);
		dayView.setCurrentItem(0);

		// 根据屏幕密度来指定选择器字体的大小
		int textSize = CommonUtil.dip2px(16);
		monthView.TEXT_SIZE = textSize;
		yearView.TEXT_SIZE = textSize;
		dayView.TEXT_SIZE = textSize;
	}
	
	private String formatTime(int time) {
		return time < 10 ? ("0" + time) : (time + "") ;
	}

	public String getTime() {
		StringBuffer sb = new StringBuffer();
		selectYearPosition = yearView.getCurrentItem();
		selectMonthPosition = monthView.getCurrentItem();
		selectDayPosition = dayView.getCurrentItem();
		sb.append(yearItems[yearView.getCurrentItem()])
		.append("-")
		.append(formatTime(monthItems[monthView.getCurrentItem()]))
		.append("-")
		.append(formatTime(dayItems[dayView.getCurrentItem()]));
		
//		int weekBegin = dateStr.indexOf("星期");
//		if (weekBegin > -1) {
//			dateStr = dateStr.substring(0,weekBegin);
//		}
//		sb.append(dateStr)
//		.append(timeItems[(timeView.getCurrentItem())]);
		return sb.toString();
	}
}
